<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>CSV文件导入工具</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
            background-color: #f5f5f5;
            padding: 20px;
            line-height: 1.6;
        }

        .container {
            max-width: 1200px;
            margin: 0 auto;
            background: white;
            border-radius: 10px;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
            overflow: hidden;
        }

        .header {
            background: #42b983;
            color: white;
            padding: 20px;
            text-align: center;
        }

        .header h1 {
            margin: 0;
            font-size: 24px;
        }

        .content {
            padding: 30px;
        }

        .upload-section {
            text-align: center;
            padding: 30px;
            border: 2px dashed #ddd;
            border-radius: 8px;
            margin-bottom: 30px;
            background-color: #fafafa;
        }

        .file-input-wrapper {
            position: relative;
            display: inline-block;
            overflow: hidden;
            margin: 20px 0;
        }

        .file-input-wrapper input[type="file"] {
            position: absolute;
            left: 0;
            top: 0;
            opacity: 0;
            width: 100%;
            height: 100%;
            cursor: pointer;
        }

        .btn {
            display: inline-block;
            padding: 12px 24px;
            background: #42b983;
            color: white;
            border: none;
            border-radius: 4px;
            font-size: 16px;
            cursor: pointer;
            transition: background 0.3s;
        }

        .btn:hover {
            background: #359c6d;
        }

        .btn-danger {
            background: #e74c3c;
        }

        .btn-danger:hover {
            background: #c0392b;
        }

        .file-info {
            display: flex;
            align-items: center;
            justify-content: space-between;
            padding: 15px;
            background-color: #e8f5e9;
            border-radius: 4px;
            margin: 20px 0;
        }

        .file-info span {
            flex: 1;
            margin: 0 15px;
            text-align: left;
            overflow: hidden;
            text-overflow: ellipsis;
            white-space: nowrap;
        }

        .data-section {
            margin-top: 30px;
        }

        .data-section h2 {
            margin-bottom: 20px;
            color: #333;
            border-bottom: 1px solid #eee;
            padding-bottom: 10px;
        }

        .table-container {
            overflow-x: auto;
            max-height: 500px;
            overflow-y: auto;
        }

        table {
            width: 100%;
            border-collapse: collapse;
            margin-bottom: 20px;
        }

        th, td {
            border: 1px solid #ddd;
            padding: 12px 15px;
            text-align: left;
        }

        th {
            background-color: #f8f9fa;
            position: sticky;
            top: 0;
        }

        tr:nth-child(even) {
            background-color: #f8f9fa;
        }

        tr:hover {
            background-color: #e8f5e9;
        }

        .data-info {
            text-align: right;
            color: #666;
            font-size: 14px;
            padding: 10px 0;
        }

        .empty-state {
            text-align: center;
            padding: 50px 20px;
            color: #999;
        }

        .empty-state .icon {
            font-size: 60px;
            margin-bottom: 20px;
            color: #ccc;
        }

        .instructions {
            background: #e3f2fd;
            padding: 20px;
            border-radius: 8px;
            margin-bottom: 30px;
        }

        .instructions h3 {
            margin-top: 0;
            color: #1976d2;
        }

        .instructions ul {
            padding-left: 20px;
        }

        .instructions li {
            margin-bottom: 10px;
        }

        @media (max-width: 768px) {
            .content {
                padding: 15px;
            }
            
            th, td {
                padding: 8px 10px;
                font-size: 14px;
            }
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="header">
            <h1>CSV文件导入工具</h1>
        </div>
        
        <div class="content">
            <div class="instructions">
                <h3>使用说明</h3>
                <ul>
                    <li>点击"选择CSV文件"按钮选择要导入的CSV文件</li>
                    <li>支持标准CSV格式，包括带引号的字段和逗号分隔符</li>
                    <li>文件解析后会以表格形式展示数据</li>
                    <li>可以随时点击"清除"按钮重新选择文件</li>
                </ul>
            </div>
            
            <div class="upload-section">
                <div class="file-input-wrapper">
                    <button class="btn">选择CSV文件</button>
                    <input type="file" id="fileInput" accept=".csv">
                </div>
                <p>请选择一个CSV格式的文件进行导入</p>
                
                <div id="fileInfo" class="file-info" style="display: none;">
                    <span id="fileName"></span>
                    <button class="btn btn-danger" id="clearBtn">清除</button>
                </div>
            </div>
            
            <div id="dataSection" class="data-section" style="display: none;">
                <h2>CSV数据预览</h2>
                <div class="table-container">
                    <table id="csvTable">
                        <thead id="tableHeader">
                        </thead>
                        <tbody id="tableBody">
                        </tbody>
                    </table>
                </div>
                <div class="data-info" id="dataInfo"></div>
            </div>
            
            <div id="emptyState" class="empty-state">
                <div class="icon">📁</div>
                <h3>暂无数据</h3>
                <p>请选择一个CSV文件开始导入</p>
            </div>
        </div>
    </div>

    <script>
        document.addEventListener('DOMContentLoaded', function() {
            const fileInput = document.getElementById('fileInput');
            const fileInfo = document.getElementById('fileInfo');
            const fileName = document.getElementById('fileName');
            const clearBtn = document.getElementById('clearBtn');
            const dataSection = document.getElementById('dataSection');
            const emptyState = document.getElementById('emptyState');
            const tableHeader = document.getElementById('tableHeader');
            const tableBody = document.getElementById('tableBody');
            const dataInfo = document.getElementById('dataInfo');
            
            // 文件选择事件
            fileInput.addEventListener('change', function(e) {
                if (this.files && this.files[0]) {
                    const file = this.files[0];
                    fileName.textContent = file.name;
                    fileInfo.style.display = 'flex';
                    
                    const reader = new FileReader();
                    reader.onload = function(e) {
                        const csv = e.target.result;
                        parseCSV(csv);
                    };
                    reader.readAsText(file);
                }
            });
            
            // 清除文件事件
            clearBtn.addEventListener('click', function() {
                fileInput.value = '';
                fileInfo.style.display = 'none';
                dataSection.style.display = 'none';
                emptyState.style.display = 'block';
                tableHeader.innerHTML = '';
                tableBody.innerHTML = '';
            });
            
            // 解析CSV函数
            function parseCSV(csv) {
                const lines = csv.split('\n');
                if (lines.length === 0) return;
                
                // 清空之前的数据
                tableHeader.innerHTML = '';
                tableBody.innerHTML = '';
                
                // 解析标题行
                const headers = parseCSVLine(lines[0]);
                const headerRow = document.createElement('tr');
                headers.forEach(header => {
                    const th = document.createElement('th');
                    th.textContent = header;
                    headerRow.appendChild(th);
                });
                tableHeader.appendChild(headerRow);
                
                // 解析数据行
                const csvData = [];
                for (let i = 1; i < lines.length; i++) {
                    if (lines[i].trim() !== '') {
                        csvData.push(parseCSVLine(lines[i]));
                    }
                }
                
                // 填充数据到表格
                csvData.forEach(row => {
                    const tr = document.createElement('tr');
                    row.forEach(cell => {
                        const td = document.createElement('td');
                        td.textContent = cell;
                        tr.appendChild(td);
                    });
                    tableBody.appendChild(tr);
                });
                
                // 显示数据信息
                dataInfo.textContent = `共 ${csvData.length} 行数据，${headers.length} 列`;
                
                // 显示数据区域
                dataSection.style.display = 'block';
                emptyState.style.display = 'none';
            }
            
            // 解析CSV行的函数
            function parseCSVLine(line) {
                const result = [];
                let current = '';
                let inQuotes = false;
                
                for (let i = 0; i < line.length; i++) {
                    const char = line[i];
                    
                    if (char === '"' && !inQuotes) {
                        inQuotes = true;
                    } else if (char === '"' && inQuotes) {
                        inQuotes = false;
                    } else if (char === ',' && !inQuotes) {
                        result.push(current.trim());
                        current = '';
                    } else {
                        current += char;
                    }
                }
                
                result.push(current.trim());
                return result;
            }
        });
    </script>
</body>
</html>